/**
 * 
 */
package it.gommonauti.patenti.entity;

import java.util.ArrayList;

/**
 * @author Francesco Stallone ( f.stallone73@gmail.com ).
 *
 * Estabilish the category of a question. 
 */
public class Category {
	
	/**
	 * Exam parent
	 */
	private Exam parent;
	
	/**
	 * Main questions.
	 */
	private ArrayList<MainQuestion> mainQuestions;
	
	/**
	 * Max errors.
	 */
	private int maxErrors;
	
	/**
	 * Number of main question in exam for this category. 
	 */
	private int questionInExam;
	
	/**
	 * Exam level
	 */
	private Exam exam;
	
	/**
	 * Category identifier
	 */
	private int id;
	
	/**
	 * Category description.
	 */
	private String description;
	
	/**
	 * Category description language.
	 */
	private Language language;

	/**
	 * @return the id
	 */
	public int getId() {
		return id;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(int id) {
		this.id = id;
	}

	/**
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * @param description the description to set
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * @return the language
	 */
	public Language getLanguage() {
		return language;
	}

	/**
	 * @param language the language to set
	 */
	public void setLanguage(Language language) {
		this.language = language;
	}

	/**
	 * @return the exam
	 */
	public Exam getExam() {
		return exam;
	}

	/**
	 * @param exam the exam to set
	 */
	public void setExam(Exam exam) {
		this.exam = exam;
	}
	
	/**
	 * @return the questionInExam
	 */
	public int getQuestionInExam() {
		return questionInExam;
	}

	/**
	 * @param questionInExam the questionInExam to set
	 */
	public void setQuestionInExam(int questionInExam) {
		this.questionInExam = questionInExam;
	}
	
	/**
	 * @return the maxErrors
	 */
	public int getMaxErrors() {
		return maxErrors;
	}

	/**
	 * @param maxErrors the maxErrors to set
	 */
	public void setMaxErrors(int maxErrors) {
		this.maxErrors = maxErrors;
	}

	
	/**
	 * @return the mainQuestions
	 */
	public ArrayList<MainQuestion> getMainQuestions() {
		return mainQuestions;
	}

	/**
	 * @param mainQuestions the mainQuestions to set
	 */
	public void setMainQuestions(ArrayList<MainQuestion> mainQuestions) {
		this.mainQuestions = mainQuestions;
	}
	
	/**
	 * @return the parent
	 */
	public Exam getParent() {
		return parent;
	}

	/**
	 * @param parent the parent to set
	 */
	public void setParent(Exam parent) {
		this.parent = parent;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		
		StringBuffer sb = new StringBuffer();
		
		sb.append("ID " + this.id + System.getProperty("line.separator"));
		
		sb.append("DESCRIPTION " + this.description + System.getProperty("line.separator"));
		
		sb.append("MAX_ERRORS " + this.maxErrors + System.getProperty("line.separator"));
		
		sb.append("QUESTION IN EXAM " + this.questionInExam + System.getProperty("line.separator"));
		
		sb.append("Exam = " + System.getProperty("line.separator"));
		if (this.getExam() != null ) sb.append(this.getExam().toString());
		else sb.append("NULL");
		
		return sb.toString();
	}

	/**
	 * Estabilish category success. 
	 */
	public boolean isSuccess(){
		
		if (mainQuestions == null) return false;
		return getNumUserFails() + getNumUserNoAnswer() < maxErrors;
		
	}
	
	/**
	 * Error count
	 */
	public int getNumUserFails(){
		
		int count = 0;
		if (mainQuestions == null) return count;
		
		for (MainQuestion mainQuestion : mainQuestions) {
			count += mainQuestion.getNumUserFails();
		}
		
		return count;
	}
	
	/**
	 * Question count. (SubQuestion count)
	 */
	public int getSubQuestionsCount(){
		int count = 0;
		
		if (mainQuestions == null) return 0;
		
		for (MainQuestion mainQuestion : mainQuestions) {
			count += mainQuestion.getSubQuestionsCount();
		}
		
		return count;
	}
	
	/**
	 * Get no answer question count
	 */
	public int getNumUserNoAnswer(){
		int count = 0;
		
		if (mainQuestions == null) return 0;		
		for (MainQuestion mainQuestion : mainQuestions) {
			count += mainQuestion.getNumUserNoAnswer();
		}
		
		return count;
	}

	/**
	 * Get no answer question count
	 */
	public int getNumUserSuccess(){
		int count = 0;
		
		if (mainQuestions == null) return 0;		
		for (MainQuestion mainQuestion : mainQuestions) {
			count += mainQuestion.getNumUserSuccess();
		}
		
		return count;
	}
}
